//
//  HKLLViewController.m
//  uplife
//
//  Created by ZhangCheng on 2018/12/25.
//  Copyright © 2018年 ZhangCheng. All rights reserved.
//

#import "HKLLViewController.h"

@interface HKLLViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,weak)IBOutlet UIView *headerView;
@property(nonatomic,weak)IBOutlet UITableView *tableView;
@property(nonatomic,assign)NSInteger currentYear;
@property(nonatomic,assign)NSInteger currentSelectedYear;
@property(nonatomic,weak)IBOutlet UILabel *nianFenLabel;
@property(nonatomic,strong)NSArray *dataArray;
@property (weak, nonatomic) IBOutlet UILabel *zongJiJinELabel;
@property (weak, nonatomic) IBOutlet UILabel *zongJiJinELabel2;

@end

@implementation HKLLViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupHeaderView];
    
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [gregorian components: unitFlags
                                          fromDate:dt];
    self.currentSelectedYear = self.currentYear = comp.year;
    [self.nianFenLabel setText:[NSString stringWithFormat:@"%@年",@(self.currentSelectedYear)]];
    [self sendRequestForLoadData];
    [self sendRequestForLoadDataOfZongJi];
}
-(void)sendRequestForLoadData{
    //    NSString *urlString = [NSString stringWithFormat:@"%@/realty/room-earning.do?roomId=%@&accountDate=%@&moneyType=CNY",JSXF_HOST,self.roomId,@(self.currentSelectedYear)];
    NSString *urlString = [NSString stringWithFormat:@"%@/receipts/list.do?paymentYear=%@&moneyType=%@",JSXF_HOST,@(self.currentSelectedYear),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"]];
    
    [SVProgressHUD show];
    
    [ZCWebServiceHelper requestWithMethod:GET andUrlStr:urlString andParameters:nil andFinished:^(id response, NSURLSessionDataTask *dataTask) {
        [SVProgressHUD dismiss];
        if (![response[@"data"] isKindOfClass:[NSString class]]) {
            NSDictionary *dic = response;
            self.dataArray = dic[@"data"][@"dataList"];
            [self.tableView reloadData];
        }
        
        //        self.dataDic = dic[@"data"];
        //        [self resetView];
    }];
}
-(void)sendRequestForLoadDataOfZongJi{
    //    NSString *urlString = [NSString stringWithFormat:@"%@/realty/room-earning.do?roomId=%@&accountDate=%@&moneyType=CNY",JSXF_HOST,self.roomId,@(self.currentSelectedYear)];
    NSString *urlString = [NSString stringWithFormat:@"%@/receipts/total.do?paymentYear=%@&moneyType=%@",JSXF_HOST,@(self.currentSelectedYear),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"]];
    
    [SVProgressHUD show];
    
    [ZCWebServiceHelper requestWithMethod:GET andUrlStr:urlString andParameters:nil andFinished:^(id response, NSURLSessionDataTask *dataTask) {
        [SVProgressHUD dismiss];
        if (![response[@"data"] isKindOfClass:[NSString class]]) {
            NSDictionary *dic = response[@"data"];
            self.zongJiJinELabel.text = [NSString stringWithFormat:@"%@",FORMAT_PRICE(dic[@"total"])];
            self.zongJiJinELabel2.text = [NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"targetTotal"])];
            self.zongJiJinELabel2.hidden = [self.zongJiJinELabel2.text hasPrefix:@"(JPY"];
        
        }
        
        //        self.dataDic = dic[@"data"];
        //        [self resetView];
    }];
}

-(IBAction)qianYiNianAction{
    self.currentSelectedYear--;
    [self.nianFenLabel setText:[NSString stringWithFormat:@"%@年",@(self.currentSelectedYear)]];
    [self sendRequestForLoadData];
    [self sendRequestForLoadDataOfZongJi];
}

-(IBAction)houYiNianAction{
    self.currentSelectedYear++;
    [self.nianFenLabel setText:[NSString stringWithFormat:@"%@年",@(self.currentSelectedYear)]];
    [self sendRequestForLoadData];
    [self sendRequestForLoadDataOfZongJi];
}

-(void)setupHeaderView{
    UIView *view = self.headerView;
    
    view.layer.cornerRadius = 8; // 圆角的弧度
    
    view.layer.masksToBounds = YES;
    view.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.17].CGColor;
    view.layer.shadowOffset = CGSizeMake(0,0);
    view.layer.shadowOpacity = 1;
    view.layer.shadowRadius = 14;
    
    CAGradientLayer *gl = [CAGradientLayer layer];
//    gl.frame = CGRectMake(view.superview.frame.origin.x, view.superview.frame.origin.y, [UIScreen mainScreen].bounds.size.width - 2 * view.superview.frame.origin.x, view.superview.frame.size.height);
    gl.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, [UIScreen mainScreen].bounds.size.width - 20.0f, view.frame.size.height);
    gl.startPoint = CGPointMake(0, 0);
    gl.endPoint = CGPointMake(1, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:209/255.0 green:171/255.0 blue:103/255.0 alpha:0.73].CGColor,(__bridge id)[UIColor colorWithRed:182/255.0 green:139/255.0 blue:62/255.0 alpha:0.73].CGColor];
    gl.locations = @[@(0.0),@(1.0)];

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:gl.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(8, 8)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    //设置大小
//    maskLayer.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 2 * view.frame.origin.x, view.frame.size.height);
    maskLayer.frame = gl.bounds;
    //设置图形样子
    maskLayer.path = maskPath.CGPath;
    gl.mask = maskLayer;
    [view.layer insertSublayer:gl atIndex:0];

    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count + 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:(indexPath.row==0?@"cell2":@"cell3") forIndexPath:indexPath];
    if (indexPath.row!=0) {
        NSDictionary *dic = self.dataArray[indexPath.row - 1];
        UILabel *label1 = [cell viewWithTag:1];
        UILabel *label2 = [cell viewWithTag:2];
        UILabel *label3 = [cell viewWithTag:3];
        UILabel *label4 = [cell viewWithTag:4];

        label1.text = [NSString stringWithFormat:@"%@",dic[@"paymentTime"]];
        label2.text = [NSString stringWithFormat:@"%@",dic[@"profitInterval"]];
        if ([[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"] isEqualToString:@"JPY"]) {
            label3.text = [NSString stringWithFormat:@"%@",FORMAT_PRICE(dic[@"money"])];
        }else{
            label3.text = [NSString stringWithFormat:@"%@\n(%@%@)",FORMAT_PRICE(dic[@"money"]),[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"targetMoney"])];
        }
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:label3.text attributes:@{NSFontAttributeName: [UIFont fontWithName:@"HiraginoSans-W3" size: 14],NSForegroundColorAttributeName: [UIColor colorWithRed:46.0f/255.0f green:52.0f/255.0f blue:65.0f/255.0f alpha:1.0f]}];
        
        [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:136/255.0 green:138/255.0 blue:143/255.0 alpha:1.0]} range:[label3.text rangeOfString:[NSString stringWithFormat:@"(%@%@)",[ZCTool sharedInstance].currentSelectedHuoBiDic[@"code"],FORMAT_PRICE(dic[@"targetMoney"])]]];
        label3.attributedText = string;
        NSNumber *statusNum = dic[@"status"];
        label4.text = statusNum.integerValue==1?@"已汇":@"未汇款";

    }
    return cell;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
